package org.erepublik.company.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.erepublik.company.EAlliance;
import org.erepublik.company.ECountry;
import org.erepublik.company.EWorld;
import org.erepublik.company.Location;

/**
 * Klasa z narzedziami
 *
 * @author Kacper Gorski 'Railman' railman85@gmail.com
 */
public class Utils {

    /**
     * @param is
     * @return Odczytuje jak najdluzszy string z podanego strumienia
     * @throws IOException
     */
	public static String convertStreamToString(InputStream is) throws IOException {
		/*
		 * To convert the InputStream to String we use the BufferedReader.readLine() method. We iterate until the BufferedReader
		 * return null which means there's no more data to read. Each line will appended to a StringBuilder and returned as String.
		 */
		if (is != null) {
			StringBuilder sb = new StringBuilder();
			String line;

			try {
				BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
				while ((line = reader.readLine()) != null) {
					sb.append(line).append("\n");
				}
			} finally {
				is.close();
			}
			return sb.toString();
		} else {
			return "";
		}
	}
	
	
	public static double round(double value, int digits) {
	    double temp = 1.0;
	    for (int i=0; i < digits; i++) {
	        temp *= 10;
	    }
	    
	    return Math.round(value*temp) / temp;
	}

	
	public static boolean contains(Object object, Object[] objects) {
	    for (Object obj : objects) {
            if (obj == object)
                return true;
        }
  
        return false;
	}
	
	
	private static Location[] location = null;
	public static Location[] getAllLocations() {
	    if (location!=null)
	        return location;
	    
	    location = new Location[ ECountry.values().length + EAlliance.values().length + 1 ];
	    for (int i=0; i < ECountry.values().length; i++) {
	        location[i] = ECountry.values()[i];
	    }
	    for (int i=0; i < EAlliance.values().length; i++) {
	        location[i + ECountry.values().length] = EAlliance.values()[i];
	    }
	    location[EAlliance.values().length + ECountry.values().length] = EWorld.GLOBAL;
	    
	    return location;
	}
	
	
	
	   public static Location[] getNewCountries() {
	        Location[] location = null;
	        
	        location = new Location[ 7 ];
	        location[0] = ECountry.BELARUS;
	        location[1] = ECountry.NEW_ZELAND;
	        location[2] = ECountry.MACEDONIA;
	        location[3] = ECountry.MONTENEGRO;
	        location[4] = ECountry.TAIWAN;
	        location[5] = ECountry.CYPRUS;
	        location[6] = EAlliance.NEW_COUNTRIES;


	        
	        return location;
	    }


    public static Location getLocationFromString(String country) {
        if (country.equals("GLOBAL"))
            return EWorld.GLOBAL;
        for (int i=0; i < EAlliance.values().length; i++) {
            if (country.equals( EAlliance.values()[i].toString() ))
                return EAlliance.values()[i];
        }
        
        return ECountry.valueOf(country);
    }
	
	

}
